package edu.ynu.se.xiecheng.achitectureclass.project.controller;

import edu.ynu.se.xiecheng.achitectureclass.common.controller.LogicController;
import edu.ynu.se.xiecheng.achitectureclass.project.dao.AirTemperatureDataDAO;
import edu.ynu.se.xiecheng.achitectureclass.project.po.entity.AirTemperatureData;
import edu.ynu.se.xiecheng.achitectureclass.project.po.vo.AirTemperatureVO;
import edu.ynu.se.xiecheng.achitectureclass.project.po.vo.EnvironmentVO;
import edu.ynu.se.xiecheng.achitectureclass.project.service.AirTemperatureDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/at")
@Api(tags = "空气温度数据管理")
public class AirTemperatureDataController extends LogicController<AirTemperatureDataService, AirTemperatureDataDAO, AirTemperatureData, Long> {

    public AirTemperatureDataController(AirTemperatureDataService ls) {
        super(ls);
    }

    @ApiOperation("从传感器API获取空气温度数据并存储到数据库")
    @PostMapping("/fetch")
    public ResponseEntity<String> fetchAndStorePHData() {
        try {
            service.fetchAndSaveAirTemperatureData(); // 调用 Service 层方法
            return ResponseEntity.ok("空气温度数据成功获取并存储！");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.internalServerError()
                    .body("获取并存空气温度数据时发生错误：" + e.getMessage());
        }
    }

    @ApiOperation("获取最近7天的空气温度数据")
    @GetMapping("/latest")
    public ResponseEntity<AirTemperatureVO> getLatestAirTemperatureData() {
        try {
            AirTemperatureVO data = service.getLatestAirTemperatureData();
            return ResponseEntity.ok(data);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    @GetMapping("/current")
    @ApiOperation("获取实时的空气温度数据")
    public ResponseEntity<EnvironmentVO> getCurrentAirTemperature() {
        try {
            EnvironmentVO data = service.getCurrentAirTemperature();
            return ResponseEntity.ok(data);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

}
